package com.fowo.api.service.handleWork;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.fowo.api.model.ship.plans.ShipPlansDuplicationAction;
import com.fowo.api.model.ship.plans.ShipPlansVo;
import com.fowo.api.model.ship.plans.sub.ShipPlansSubItemVo;
import com.fowo.api.service.ShipPlansService;
import com.fowo.api.sys.model.NewSerialRequest;
import com.fowo.api.sys.service.SysSerialService;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Component
public class ShipPlansDuplication implements ShipPlansDuplicationAction {

    @Resource
    private ShipPlansService service;
    @Resource
    protected SysSerialService sysSerialService;

    @Override
    public void run(Long id) throws Exception {
        final ShipPlansVo shipPlansVo = service.getVoById(id);
        Assert.notNull(shipPlansVo,"单据不存在");
        ShipPlansVo model = new ShipPlansVo();
        BeanUtil.copyProperties(shipPlansVo,model,"id","createBatchNumber");
        model.setStatus("1");
        for(ShipPlansSubItemVo subItemVo : model.getProductList()){
            subItemVo.setIsUrgent(false);
            subItemVo.setIsLock(false);
            subItemVo.setId(null);
            subItemVo.setOrderSn(null);
            subItemVo.setParentShipPlans(null);
            // 填充序列号
            subItemVo.setOrderSn(
                    sysSerialService.getNewSerial(
                            new NewSerialRequest(null, "ShipPlansSub.orderSn")
                    )
            );
        }
        service.create(model);
    }
}
